var nowDate = getDay(0);
var layer;
var longitude;
var latitude;
//var longitude="112.546997";
//var latitude="37.862444";
var date = getDay(0);  //今日
var jobnumber = sessionStorage.getItem("jobnumber")==null?"0":sessionStorage.getItem("jobnumber");
var job = sessionStorage.getItem("job")==null?"0":sessionStorage.getItem("job");
var corp = sessionStorage.getItem('corp');
var photoUrl = '',pandianid = GetQueryString('pandianid');
layui.use(['form','layer'], function(){
	layer = layui.layer;
	
//	inmap(latitude,longitude);
});
var _config ;
$.ajax({
	type : "post",
	data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26'),"corp":corp}),
	contentType: "application/json; charset=utf-8",
	async: false,
	url : "../../dingding/getconfig",
	dataType : "json",
	success : function(suc) {
		_config = suc.config;
	}
}); 
dd.config({
	agentId : _config.agentid,
	corpId : _config.corpId,
	timeStamp : _config.timeStamp,
	nonceStr : _config.nonceStr,
	signature : _config.signature,
	jsApiList : [ 'device.geolocation.get','biz.map.locate','biz.map.view','biz.map.search','biz.util.uploadImageFromCamera' ]
});
dd.ready(function() {
	//获取位置
    dd.device.geolocation.get({
		    targetAccuracy : 200,
		    coordinate : 1,
		    withReGeocode : true,
		    useCache:false, //默认是true，如果需要频繁获取地理位置，请设置false
		    onSuccess : function(result) {
				_result = result;
				latitude = _result.latitude;
				longitude = _result.longitude;
				$('#address').text(_result.address);
				inmap(latitude,longitude);

		    },
		    onFail : function(err) {
				alert(JSON.stringify(err));
			}
		});
    $('#paizhao').on('click',function(){
			
			dd.biz.util.uploadImageFromCamera({
				    compression: false,//(是否压缩，默认为true)
				    quality: 100, // 图片压缩质量, 
				    resize: 100, // 图片缩放率
				    stickers: {   // 水印信息
					        time: nowDate,
					        dateWeather: "",
					        username: sessionStorage.getItem("name"),
					        address: $('#address').text()
					    },
					    onSuccess : function(result) {
						//photoUrl = JSON.stringify(result);
						photoUrl = result[0];
						$('#paizhao').empty();
						$('#paizhao').append('<img src="'+result+'">');
						$('#paizhao').off('click');
						         //onSuccess将在图片上传成功之后调用
						        /*
			        [
			          'http://gtms03.alicdn.com/tps/i3/TB1VF6uGFXXXXalaXXXmh5R_VXX-237-236.png'
			        ]
			        */
						    },
						    onFail : function(err) {}
			});
	});
    
});

dd.error(function(err) {
	layer.closeAll();
	alert('dd error: ' + JSON.stringify(err));
});

function inmap(latitude,longitude){
	var map = new AMap.Map('weizhi',{
		dragEnable: false,
	    zoom: 15,
	    center: [longitude, latitude]
	});
	 map.setMapStyle('amap://styles/light');
	  var marker = new AMap.Marker({
	        map: map,
			position: [longitude, latitude],
	        icon: new AMap.Icon({            
	            //size: new AMap.Size(40, 50),  //图标大小
	            image: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png"
	            //imageOffset: new AMap.Pixel(0, 0)
	        })        
	    });
//	  var _onClick = function(e){
//		    new AMap.Marker({
//		        position : e.lnglat,
//		        map : map
//		    })
//		};
//		map.on('click',_onClick);
	  map.on('click', function() {
		  dd.biz.map.search({
			    latitude: _result.latitude, // 纬度
			    longitude: _result.longitude, // 经度
			    scope: 300, // 限制搜索POI的范围；设备位置为中心，scope为搜索半径
			 
			    onSuccess: function (poi) {
			    	latitude = poi.latitude;
					longitude = poi.longitude;
					$('#address').text(poi.snippet);
			        /* result 结构 
			        {
			            province: 'xxx', // POI所在省会
			            provinceCode: 'xxx', // POI所在省会编码
			            city: 'xxx', // POI所在城市
			            cityCode: 'xxx', // POI所在城市
			            adName: 'xxx', // POI所在区名称
			            adCode: 'xxx', // POI所在区编码
			            distance: 'xxx', // POI与设备位置的距离
			            postCode: 'xxx', // POI的邮编
			            snippet: 'xxx', // POI的街道地址
			            title: 'xxx', // POI的名称
			            latitude: 39.903578, // POI的纬度
			            longitude: 116.473565, // POI的经度
			        }*/
			    },
			    onFail: function (err) {
			    }
			});
	  });
}
$(document).ready(function(){
	$('#name').text(sessionStorage.getItem("name"));
	$('#shopname').text(sessionStorage.getItem("shopname"));
	$('#saveinfo').on('click',function(){
		saveinfo();
	});
});
//插入盘点到店信息
function saveinfo(){
	if(photoUrl == ''){
		layer.msg('必须拍摄照片')
	}else{
		showloading();
		var sendData = {};
		sendData.pandianid=pandianid;
		sendData.shopcode=sessionStorage.getItem("shopcode");
		sendData.shopname=sessionStorage.getItem("shopname");
		sendData.shoplongitude=sessionStorage.getItem("shoplongitude");
		sendData.shoplatitude=sessionStorage.getItem("shoplatitude");
		sendData.shopaddress=sessionStorage.getItem("shopaddress");
		sendData.latitude=latitude;
		sendData.longitude=longitude;
		sendData.address=$('#address').text();
		sendData.img= photoUrl;
		sendData.lrr=sessionStorage.getItem("userId");
		$.ajax({
			url: "../pandianevery_jd/replacePandianJianchaDaodian",
			type : 'POST',
			data:JSON.stringify(sendData),
			contentType: "application/json; charset=utf-8",
			dataType : "json",
			success : function(suc) {
				console.log(suc)
				try {
					if(suc.success){
						window.location.href='toJiancha_index_list?pandianid='+pandianid;
					}else{
						layer.msg(suc.message)
					}
				} catch (e) {
					console.log(e);
				} finally{
					hideloading();
				}
				
			}
		});
	}
	
}

